package com.ril.financial.model;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;

public class TransactionCollection<T extends Transaction> extends ArrayList<T> {

    public BigDecimal calculateTotal() {
        BigDecimal total = new BigDecimal(0);

        for (Transaction transaction : this) {
            if (transaction.amount != null) {
                total = total.add(transaction.amount);
            }
        }
        return total;
    }

    public Date getBeginDate() {
        if (this.size() == 0) {
            return null;
        }
        T firstTransaction = Collections.min(this, new Comparator<T>() {
            public int compare(T o1, T o2) {

                return o1.date.compareTo(o2.date);
            }
        });
        return firstTransaction.date;
    }

    public Date getEndDate() {
        if (this.size() == 0) {
            return null;
        }
        T firstTransaction = Collections.max(this, new Comparator<T>() {
            public int compare(T o1, T o2) {

                return o1.date.compareTo(o2.date);
            }
        });
        return firstTransaction.date;
    }
}
